/**
 * 工具类
 */

export default {

    /**
     * 判断非空
     * @param val
     * @returns {Boolean}
     */
    isEmpty: function(val) {


        val = (val + '').replace(/\u200B/g, ""); // \u200B为&#8203;无宽度空格
        if (val == null || val == "null")
            return true;
        if (val == undefined || val == 'undefined')
            return true;
        if (val == "")
            return true;
        if (val.length == 0)
            return true;
        if (!/[^(^\s*)|(\s*$)]/.test(val))
            return true;
        return false;
    },

    isNotEmpty: function(val) {

        var that = this;
        return !that.isEmpty(val);
    },

    // 判断一个字符串是否为数字
    isNum(str) {
        return !isNaN(str);
    },
    // 判断是否为手机号格式
    isMobile(str) {
        return /^1[34578][0-9]{9}$/.test(str);
    },
    // 判断是否为邮箱格式
    isEmail(str) {
        return /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(str);
    },
    /**
     * @description 获取url地址中的参数，将其转化成hash对象
     * @param {String} url 网页地址
     */
    getUrlParams: function(url) {

        url = this.isEmpty(url) ? location.href : url;
        var spliters = url.split('?')
            /*,
                        paramPart = splitUrl[splitUrl.length - 1],
                        paramsArr = paramPart.split('&')*/
            ,
            paramsArr = [];

        var params = {};

        for (var i = 1; i < spliters.length; i++) {
            var spliter = spliters[i],
                paramPart = spliter.split('&');

            for (var j = 0; j < paramPart.length; j++) {
                paramsArr.push(paramPart[j]);
            }
        }

        //      if(splitUrl.length > 1) {

        for (var i = 0, l = paramsArr.length, param, name, val; i < l; i++) {

            param = paramsArr[i].split('=');
            name = param[0];
            val = param[1] || '';

            if (typeof name === 'string' && name !== '') {

                if (params[name]) {

                    if (typeof params[name] === 'string') {

                        params[name] = [params[name]];
                    }

                    params[name].push(val);
                } else {

                    params[name] = val;
                }
            }
        }
        //      }

        return params;
    },

    getQueryString(params, url) {
        var url = url || location.href;
        var search = url.split('?')[1];
        if (search) {
            var n = new RegExp("(^|&)" + params + "=([^(&|#)]*)((&|#)|$)", "i"),
                r = search.match(n);
            return null != r ? r[2] : null
        }
        return null;
    },

    /**
     * @description 设置localStorage的值
     * @param {String/Object} name 名称
     * @param {Object} val 值
     */
    setLs: function(name, val) {

        if (typeof name === 'object') {

            for (var k in name) {

                if (name.hasOwnProperty(k)) {

                    localStorage.setItem(k, name[k]);
                }
            }
        } else {

            localStorage.setItem(name, val);
        }

        return this;
    },

    /**
     * @description 设置localStorage的值
     * @param {String} name 名称
     */
    getLs: function(name) {

        return localStorage.getItem(name);

    },
    //过滤空字符串
    filterEmptyProp(obj) {
        let o = JSON.parse(JSON.stringify(obj));
        for (var i in o) {
            let item = o[i];
            if (this.isEmpty(item)) {
                delete o[i]
            } else if (typeof item === 'object') {
                item = this.filterEmptyProp(item);
            }
        }
        return o;
    },
    //获取当前时间
    getDate() {
        var now = new Date();
        var year = now.getFullYear();
        var month = now.getMonth() + 1;
        var day = now.getDate();
        var hh = now.getHours();
        var mm = now.getMinutes();
        var ss = now.getSeconds();
        var clock = year + "-";
        if (month < 10) clock += "0";
        clock += month + "-";
        if (day < 10) clock += "0";
        clock += day + " ";
        if (hh < 10) clock += "0";
        clock += hh + ":";
        if (mm < 10) clock += "0";
        clock += mm + ":";
        if (ss < 10) clock += "0";
        clock += ss;
        return clock;
    },
    nowDate(AddDayCount) {
        var dd = new Date();
        dd.setDate(dd.getDate() + AddDayCount); //获取AddDayCount天后的日期 
        var y = dd.getFullYear();
        var m = dd.getMonth() + 1;
        if (m < 10) m = "0" + m;
        var d = dd.getDate();

        if (d < 10) d = "0" + d;

        return y + "-" + m + "-" + d;
    },
    tip: function(options) {

        var opts = options || {};
        let tipContainer = document.getElementById('J-tipContainer')
        let tipContent = document.getElementById('J-tipContent')

        var timeout = opts.timeout || 1;

        if (!tipContainer) {
            let div = document.createElement('div')
            let span = document.createElement('span')
            span.setAttribute('id', 'J-tipContent')
            div.setAttribute('id', 'J-tipContainer')
            div.setAttribute('style', 'max-width:70%;min-width:200px;max-height:80%;padding:15px;text-align:center;background:rgba(0,0,0,0.6);font-size:14px;color:#fff;position:fixed;top:50%;left:50%;border-radius:5px;z-index:99999;')
                // var TIP_TEMP = [
                //         "<div style='max-width:70%;min-width:200px;max-height:80%;padding:15px;text-align:center;background:rgba(0,0,0,0.6);font-size:14px;color:#fff;position:fixed;top:50%;left:50%;border-radius:5px;opacity:0;filter:alpha(opacity=0);z-index:99999;'>",
                //             "<span></span>",
                //         "</div>"
                //     ].join("");
            div.appendChild(span)
            tipContainer = div
            tipContent = span
            document.body.appendChild(div);

        }

        tipContent.innerHTML = opts.content || ''

        tipContainer.style.display = 'block'
            // console.log(tipContainer.clientHeight)
        tipContainer.style.marginTop = `${tipContainer.clientHeight / 2 * -1}px`
        tipContainer.style.marginLeft = `${tipContainer.clientWidth / 2 * -1}px`
        tipContainer.onclick = () => {

            tipContainer.style.display = 'none'
            if (typeof opts.callback == "function") {
                opts.callback()
            }
            clearTimeout(this.tipTimer)
        }

        clearTimeout(this.tipTimer)

        this.tipTimer = setTimeout(function() {
            tipContainer.style.display = 'none'
            if (typeof opts.callback == "function") {
                opts.callback()
            }
        }, timeout * 1000);
    },
}